class Person:
    def __init__(self,age,name,gender):
        #实例属性
        self.name = name
        self.age = age
        self.gender = gender

        #实例方法
    def introduce(self):
        return f"大家好，我叫{self.name}，今年{self.age}岁，是{self.gender}生。"
    

#课程类
class Course:
    def __init__(self, name):
        self.name = name
        self.students = []  # 存储选修该课程的学生

    def add_student(self, student):
        if student not in self.students:
            self.students.append(student)
            return f"课程《{self.name}》成功添加学生{student.name}"
        return f"学生{student.name}已在课程《{self.name}》中"

#学生类，继承Person类
class Student(Person):
    def __init__(self,name,age,gender,student_id,grade):
        super().__init__(name,age,gender)
        self.student_id = student_id
        self.grade = grade
        self.courses = []


    def introduce(self):
        return f"大家好，我叫{self.name}，是{self.grade}年级的学生，学号是{self.student_id}。"

    #学生持有的方法
    def enroll_course(self, course):
        if course not in self.courses:
            self.courses.append(course)
            result = course.add_student(self)
            return f"{self.name}成功选修了《{course.name}》课程，{result}"
        return f"{self.name}已经选修了《{course.name}》课程"

    def list_courses(self):
        if self.courses:
            course_names = [course.name for course in self.courses]
            return f"{self.name}选修的课程有：{', '.join(course_names)}"
        return f"{self.name}还没有选修任何课程"
    

class Administrator(Person):
    def __init__(self,name,age,gender,admin_id):
        super().__init__(age,gender,name)
        self.admin_id = admin_id

    def introduce(self):
        return f"大家好，我是管理员{self.name},工号是{self.admin_id}"
    
    def add_student_to_grade(self, student, grade):
        """将学生添加到指定年级"""
        result = grade.add_student(student)
        return f"管理员{self.name}操作：{result}"

    def add_course_to_grade(self, course, grade):
        """将课程添加到指定年级"""
        result = grade.add_course(course)
        return f"管理员{self.name}操作：{result}"

    def view_grade_info(self, grade):
        """查看年级的学生和课程信息"""
        student_info = grade.list_students()
        course_info = grade.list_courses()
        return f"【{grade.grade_name}年级信息】\n{student_info}\n{course_info}"
    


class Grade:
    def __init__(self, grade_name):
        self.grade_name = grade_name  
        self.students = []        
        self.courses = []             

    def add_student(self, student):
        """将学生添加到年级"""
        if student not in self.students:
            self.students.append(student)
            return f"学生{student.name}已成功加入{self.grade_name}年级"
        return f"学生{student.name}已在{self.grade_name}年级中"

    def add_course(self, course):
        """将课程添加到年级"""
        if course not in self.courses:
            self.courses.append(course)
            return f"课程《{course.name}》已成功添加到{self.grade_name}年级"
        return f"课程《{course.name}》已在{self.grade_name}年级中"

    def list_students(self):
        """列出年级所有学生"""
        if self.students:
            student_names = [stu.name for stu in self.students]
            return f"{self.grade_name}年级的学生有：{', '.join(student_names)}"
        return f"{self.grade_name}年级暂无学生"

    def list_courses(self):
        """列出年级所有课程"""
        if self.courses:
            course_names = [course.name for course in self.courses]
            return f"{self.grade_name}年级的课程有：{', '.join(course_names)}"
        return f"{self.grade_name}年级暂无课程"
    



